/*
 * @Author: szx
 * @Date: 2022-02-20 12:27:58
 * @LastEditTime: 2022-02-20 12:47:57
 * @Description:
 * @FilePath: \leetcode\900-999\969\969.js
 */
/**
 * @param {number[]} arr
 * @return {number[]}
 */
var pancakeSort = function (arr) {
    // n 为数组的长度
    const res = [];
    for (let n = arr.length; n > 1; n--) {
        let index = 0;
        for (let i = 1; i < n; i++) if (arr[i] >= arr[index]) index = i;
        if (index === n - 1) continue;
        reverse(arr, index);
        reverse(arr, n - 1);
        res.push(index + 1);
        res.push(n);
    }
    return res;
};

const reverse = (arr, end) => {
    for (let i = 0, j = end; i < j; i++, j--) {
        let temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
};
